﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.Controller;
using CRM.Entity.Extra;

namespace CRM.Web.Admin
{
    public partial class NewsList : System.Web.UI.Page
    {
        ArticleInfoController articleInfoController = new ArticleInfoController();
        int pageIndex = 1;
        int pageSize = 20;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrWhiteSpace(Request.QueryString["page"]))
                {
                    int.TryParse(Request.QueryString["page"].ToString(), out pageIndex);
                }
                BindData(); 
            }
        }

        protected void BindData()
        {
            var pageInfo = new PageInfo()
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            
            var articleInfo = articleInfoController.GetListExtra(pageInfo, 1);
            int SummaryLength = 100; //概要长度
            if (articleInfo.ListEntity != null)
            {
                foreach (var info in articleInfo.ListEntity)
                {
                    info.ArticleContent = Common.DelHTML.DelHtml(info.ArticleContent);
                    SummaryLength = info.ArticleContent.Length > 100 ? 100 : info.ArticleContent.Length;
                    info.ArticleContent = System.Web.HttpUtility.HtmlEncode(info.ArticleContent.Substring(0, SummaryLength) + "......");
                }
                this.rpNewsList.DataSource = articleInfo.ListEntity; 
                this.rpNewsList.DataBind();

                BindPager(articleInfo.RecordCount);
            }
        }


        private void BindPager(int itemCount)
        {
            string url = "/NewsList.aspx?page={0}";
           
            pagerBlock.CurrentPageIndex = pageIndex;
            pagerBlock.ItemCount = itemCount;
            pagerBlock.PageSize = pageSize;
            pagerBlock.PageCount = 3;
            pagerBlock.urlFormat = url;
        }
    }
}